layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/clerk/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
       {field: 'userId', title: '店员ID'}
      ,{field: 'name', title: '店员姓名',templet:function (record){
          return record.user.name;
              }}
      ,{field: 'phone', title: '店员手机号',templet:function (record){
                  return record.user.phone;
              }}
      ,{field: 'nickname', title: '店员昵称',templet:function (record){
                  return record.user.nickname;
              }}
  	  ,{field: 'merchantName', title: '商户姓名',templet:function (record){
		      return record.merchant.nickname;
	  }}
	  ,{field: 'merchantPhone', title: '商户手机号',templet:function (record){
	  		 return record.merchant.phone;
	  }}
      ,{field: 'placeCount', title: '管理网点数'}
	  ,{field: 'funcModelCount', title: '权限配置数'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 180, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      info:function(obj){
          layer.open({
              type: 2
              ,title: obj.data.name
              ,content: layui.setter.webRoot + '/clerk/info.html?userId='+obj.data.userId+'&merchantUserId='+obj.data.merchantUserId
              ,maxmin: true
              ,area: ['90%', '90%']
              ,btn: ['取消']
          });
      }
     ,del:function(obj){
          layer.confirm('确定要删除该用户信息吗？', function(index){
                $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/clerk/del.do',
                  data:{userId:obj.data.userId,merchantUserId:obj.data.merchantUserId},
                  dataType:"json",
                  success:function(res){
                    if(res.code == 200){
                        obj.del();
                    }else{
                        layer.msg(res.msg,{icon: 5});
                    }
                  }
                });
                layer.close(index);
	      });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('clerk_list', {})
});